﻿/// <reference path="libs/_references.js" />


(function () {
	var appLayout =
		new kendo.Layout('<div id="main-content"></div>');
	var data = persisters.get("api/");
	vmFactory.setPersister(data);

	var router = new kendo.Router();

	router.route("/stores", function () {
	    viewsFactory.getStoresView()
			.then(function (storesViewHtml) {
			    vmFactory.getStoresVM()
				.then(function (storesVM) {
				    var view =
						new kendo.View(storesViewHtml,
						{ model: storesVM });
				    appLayout.showIn("#main-content", view);
				}, function (err) {
				    //...
				});
			});
	});

	router.route("/", function () {
	    router.navigate("/stores");
	});

	router.route("/stores/:id", function (id) {
	    viewsFactory.getStoreFullView()
			.then(function (storeFullViewHtml) {
			    vmFactory.getStoreFullVM(id)
				.then(function (storeFullVM) {
				    var view =
						new kendo.View(storeFullViewHtml,
						{ model: storeFullVM });
				    appLayout.showIn("#main-content", view);
				    kendo.bind($("button"), storeFullVM);

				    //router.navigate("/profile");
				}, function (err) {
				    //...
				});
			});
	});

	router.route("/books", function () {
	    viewsFactory.getBooksView()
			.then(function (booksViewHtml) {
			    vmFactory.getBooksVM()
				.then(function (booksVM) {
				    var view =
						new kendo.View(booksViewHtml,
						{ model: booksVM });
				    appLayout.showIn("#main-content", view);
				    kendo.bind($("button"), booksVM);

				    //router.navigate("/profile");
				}, function (err) {
				    //...
				});
			});
	});

	router.route("/books/free", function () {
	    viewsFactory.getBooksView()
			.then(function (booksViewHtml) {
			    vmFactory.getBooksFreeVM()
				.then(function (booksFreeVM) {
				    var view =
						new kendo.View(booksViewHtml,
						{ model: booksFreeVM });
				    appLayout.showIn("#main-content", view);
				    kendo.bind($("button"), booksFreeVM);

				    //router.navigate("/profile");
				}, function (err) {
				    //...
				});
			});
	});

	router.route("/books/:genre", function (genre) {
	    viewsFactory.getBooksView()
			.then(function (booksViewHtml) {
			    vmFactory.getBooksGenreVM(genre)
				.then(function (booksGenreVM) {
				    var view =
						new kendo.View(booksViewHtml,
						{ model: booksGenreVM });
				    appLayout.showIn("#main-content", view);
				    kendo.bind($("button"), booksGenreVM);

				    //router.navigate("/profile");
				}, function (err) {
				    //...
				});
			});
	});

	router.route("/profile", function () {
	    if (data.users.currentUser()) {
	        router.navigate("/login");
	    }
	    else {
	        viewsFactory.getUserView()
				.then(function (userViewHtml) {
				    vmFactory.getUserVM()
                        .then(function (userVM) {
                            var view =
						        new kendo.View(userViewHtml,
						        { model: userVM });
                            appLayout.showIn("#main-content", view);
                            kendo.bind($("button"), userVM);

                            //router.navigate("/profile");
                        }, function (err) {
                            //...
                        });
				});
	    }
	});

	router.route("/login", function () {
		if (data.users.currentUser()) {
			router.navigate("/");
		}
		else {
			viewsFactory.getLoginView()
				.then(function (loginViewHtml) {
					var loginVm = vmFactory.getLoginVM(
						function () {
							router.navigate("/");
						});
					var view = new kendo.View(loginViewHtml,
						{ model: loginVm });
					appLayout.showIn("#main-content", view);
				});
		}
	});

	//only for registered users
	router.route("/special", function () {
		if (!data.users.currentUser()) {
			router.navigate("/login");
		}
	});

	$(function () {
	    appLayout.render("#app");

	    router.start();
	    
	});
}());